import Mock from 'mockjs'

// 住宿报销金额
import moneys from '@/mock/chaLv'
Mock.mock('/api/getMoneys', 'get', moneys)

// 发票类型维护
import fapiao from '@/mock/fapiao'
Mock.mock('/api/getfapiaos', 'get', fapiao)
// 添加信息
Mock.mock('/api/addfapiao', 'post', options => {
  var len = fapiao.length
  var data = JSON.parse(options.body).data
  data.id = fapiao[len - 1].id + 1
  fapiao.push(data)
  var newLen = fapiao.length
  if (newLen > len) {
    return fapiao
  } else {
    return {
      code: 500,
      data: 'error'
    }
  }
})
// 更新信息
Mock.mock('/api/putfapiao', 'put', options => {
  var data = JSON.parse(options.body).data
  var index = fapiao.findIndex(item => {
    return item.id === data.id
  })
  fapiao.splice(index, 1, data)
  return fapiao
})
// 删除信息
Mock.mock('/api/delfapiao', 'delete', options => {
  var id = JSON.parse(options.body).id
  fapiao.forEach((item, index) => {
    if (item.id === id) {
      fapiao.splice(index, 1)
    }
  })
  return fapiao
})



/** 字典 */
import TbList from './ziDian/table'
import row from './ziDian/row'
import ziDianData from './ziDian/data'
// 获取字典列表
Mock.mock('/api/getZiDian', 'get', TbList)

// 添加字典列表
Mock.mock('/api/addZDB', 'post', options => {
  var len = TbList.length
  var data = JSON.parse(options.body).data
  data.id = TbList[len - 1].id + 1
  TbList.push(data)
  var newLen = TbList.length
  if (newLen > len) {
    return TbList
  } else {
    return {
      code: 500,
      data: 'error'
    }
  }
})

// 获取字典列表列 字段
Mock.mock(`/api/getRow`, 'get', (options) => {
  console.log(JSON.parse(options.body))
  var id = JSON.parse(options.body).id
  var data = row.filter((item) => {
    return item.father === id
  })
  if (data.length > 0) {
    return data[0].row
  } else {
    return []
  }
})

// 获取全部字段值
Mock.mock(`/api/getZiDianTbdata`, 'get', (options) => {
  var id = JSON.parse(options.body).id
  var data = ziDianData.filter((item) => {
    return item.father === id
  })
  if (data.length > 0) {
    return data[0].data
  } else {
    return []
  }
})